#include "uart.h"

#define FOSC 11059200 //System frequency
#define BAUD 9600     //UART baudrate

/*Define UART parity mode*/
#define NONE_PARITY 0  //None parity
#define ODD_PARITY 1   //Odd parity
#define EVEN_PARITY 2  //Even parity
#define MARK_PARITY 3  //Mark parity
#define SPACE_PARITY 4 //Space parity

//#define PARITYBIT EVEN_PARITY //Testing even parity
#define PARITYBIT NONE_PARITY
//sbit bit9 = P22;           //P2.2 show UART data bit9
//uint8_t busy;

void InitialUart(void)
{
#if (PARITYBIT == NONE_PARITY)
    SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
    SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
    SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif

    TMOD = 0x20;                          //Set Timer1 as 8-bit auto reload mode
    TH1 = TL1 = -(FOSC / 12 / 32 / BAUD); //Set auto-reload vaule
    TR1 = 1;                              //Timer1 start run
    ES = 1;                               //Enable UART interrupt
    EA = 1;                               //Open master interrupt switch

    //SendString("STC89-90xx\r\nUart Test !\r\n");
}

void UartSendByte(unsigned char c)
{
    TI = 0;
    SBUF = c;
    while (TI == 0)
        ;
    TI = 0;
}

void UartSend(unsigned char *s)
{
    while (*s)
    {
        SBUF = *s++;
        while (TI == 0)
            ;
        TI = 0;
    }
}